﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;

namespace AlbumArtOrganizer.Plugins.Bing.Model
{
    [Export(AlbumArtOrganizer.Plugins.Bing.ImageFinder.ImageFinder.OPTIONCONTRACTNAME, typeof(AlbumArtOrganizer.Model.IOption))]
    public class OptionSafeSearch : AlbumArtOrganizer.Model.OptionEnumeration
    {
        public enum Type
        {
            Strict,
            Moderate,
            Off
        }

        public override string Title
        {
            get
            {
                return "Safe search";
            }
        }

        public override string Description
        {
            get
            {
                return "";
            }
        }

        public override IEnumerable<string> AllowedValues
        {
            get
            {
                return from OptionSafeSearch.Type item in Enum.GetValues(typeof(OptionSafeSearch.Type))
                       select item.ToString();
            }
        }

        string _value = null;
        public override string Value
        {
            get
            {
                if (String.IsNullOrEmpty(_value))
                {
                    _value = Properties.Settings.Default.OptionSafeSearch;
                }
                return _value;
            }
            set
            {
                _value = value;
                Properties.Settings.Default.OptionSafeSearch = _value;
                Properties.Settings.Default.Save();
            }
        }

        public string GetSearchString()
        {
            var option = (from OptionSafeSearch.Type item in Enum.GetValues(typeof(OptionSafeSearch.Type))
                          where item.ToString() == Value
                          select item).First();
            return option.ToSearchString();
        }
    }

    public static class OptionSafeSearchExtensions
    {
        public static string ToSearchString(this OptionSafeSearch.Type option)
        {
            switch (option)
            {
                case OptionSafeSearch.Type.Strict:
                    return "Strict";
                case OptionSafeSearch.Type.Moderate:
                    return "Moderate";
                case OptionSafeSearch.Type.Off:
                    return "Off";
            }
            throw new InvalidOperationException();
        }
    }
}
